export default {
  data() {
    return {
      lazyInitData: false, // 是否懒加载列表数据，默认在mouted的时候请求数据
      listLoading: true, // 列表loading
      pagination: {
        background: true,
        "current-page": 1,
        "page-size": 10,
        total: 0,
      },
      _originParam: {},
      _queryParam: {},
    };
  },
  computed: {
    total: {
      get() {
        return this.pagination.total;
      },
      set(val) {
        this.pagination.total = val;
      },
    },
    page() {
      return this.pagination["current-page"];
    },
    limit() {
      return this.pagination["page-size"];
    },
  },
  mounted() {
    this._queryParam = Object.assign({}, this.queryParam || {});
    this._originParam = Object.assign({}, this.queryParam || {});
    if (!this.lazyInitData) {
      this.loadDataList();
    }
  },
  methods: {
    fetchDataList() {
      console.error("页面需要实现 fetchDataList 方法请求数据");
    },
    // 手动加载数据
    loadDataList() {
      this._queryParam = Object.assign({}, this.queryParam || {});
      this._originParam = Object.assign({}, this.queryParam || {});
      this.$$fetchListWithLoading(this._queryParam);
    },
    // 重新加载分页数据
    reloadDataList() {
      this.$$fetchListWithLoading(this._queryParam);
    },
    // 重置分页数据
    resetPagination(needResetParam) {
      this.pagination["current-page"] = 1;
      if (typeof needResetParam === "boolean" && needResetParam) {
        this.queryParam = Object.assign({}, this._originParam);
        this._queryParam = Object.assign({}, this._originParam);
      } else {
        this._queryParam = Object.assign({}, this.queryParam || {});
      }
      this.$$fetchListWithLoading(this._queryParam);
    },
    async $$fetchListWithLoading(...rest) {
      this.listLoading = true;
      await this.fetchDataList(...rest);
      setTimeout(() => {
        this.listLoading = false;
      }, 700);
    },
    // 页码修改或每页数量修改
    paginationChange(type, num) {
      if (type === "pageIndex") {
        this.pagination["current-page"] = num;
        this.$$fetchListWithLoading(this._queryParam);
      } else if (type === "pageSize") {
        this.pagination["page-size"] = num;
        this.resetPagination(false);
      }
    },
    // 获取每一行序号
    getSerialNumber(index) {
      return (this.page - 1) * this.limit + (index + 1);
    },
  },
};
